/**
 * 
 */
package com.maywide.jdp.system.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.maywide.jdp.core.vo.Page;
import com.maywide.jdp.system.annotation.JSON;
import com.maywide.jdp.system.bean.RuleBean;
import com.maywide.jdp.system.entities.BusinessFunction;
import com.maywide.jdp.system.entities.Module;
import com.maywide.jdp.system.entities.Rule;
import com.maywide.jdp.system.utils.JSONEditor;

/**
 * 规则管理Service
 * 
 * @author luoyang
 * @version 1.0
 * @since JDK1.6
 */
@Controller("ruleService")
public class RuleServiceImpl implements RuleService {
	/**
	 * 规则管理Bean接口
	 */
	private RuleBean bean = null;

	/**
	 * @param bean
	 *            the bean to set
	 */
	@Autowired
	@Qualifier("ruleBean")
	public void setBean(RuleBean bean) {
		this.bean = bean;
	}

	@JSON
	@RequestMapping(value = "/system/rule/insert.json")
	@Override
	public Rule insert(@RequestParam(value = "rule") Rule rule,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.insert(rule);
		return rule;
	}

	@JSON
	@RequestMapping(value = "/system/rule/update.json")
	@Override
	public Rule update(@RequestParam(value = "rule") Rule ruleVO,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		Rule rule = null;
		String id = ruleVO.getId();
		rule = (Rule) bean.load(id);

		// 取得不可改变属性
		Module module = rule.getModule();
		List<BusinessFunction> functions = rule.getFunctions();

		ruleVO.setModule(module);
		ruleVO.setFunctions(functions);
		BeanUtils.copyProperties(ruleVO, rule);

		bean.update(rule);
		return rule;
	}

	@JSON
	@RequestMapping(value = "/system/rule/load.json")
	@Override
	public Rule load(@RequestParam(value = "id") String id,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		Rule rule = (Rule) bean.load(id);
		return rule;
	}

	@RequestMapping(value = "/system/rule/lock.json")
	@Override
	public void lock(@RequestParam(value = "id") String id,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.lock(id);
	}

	@RequestMapping(value = "/system/rule/unlock.json")
	@Override
	public void unlock(@RequestParam(value = "id") String id,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.unlock(id);
	}

	@RequestMapping(value = "/system/rule/move.json")
	@Override
	public void move(@RequestParam(value = "ids") List<String> ids,
			@RequestParam(value = "targetID") String targetID,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.move(ids, targetID);
	}

	@JSON
	@RequestMapping(value = "/system/rule/page.json")
	public Page page(
			@RequestParam(value = "first", defaultValue = "0") int first,
			@RequestParam(value = "limit", defaultValue = "20") int limit,
			@RequestParam(value = "sort", required = false) String sort,
			@RequestParam(value = "dir", required = false) String dir,
			@RequestParam(value = "moduleID", required = false) String moduleID,
			@RequestParam(value = "key", required = false) String key,
			@RequestParam(value = "active", required = false) String active,
			@RequestParam(value = "code", required = false) String code,
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "handle", required = false) String handle,
			@RequestParam(value = "serverClass", required = false) String serverClass,
			@RequestParam(value = "serverFunction", required = false) String serverFunction,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		Map<String, Object> conditions = new HashMap<String, Object>();

		if (key != null) {
			key = new StringBuffer("%").append(key).append("%").toString();
			conditions.put("key", key);
		}

		if (active != null)
			conditions.put("active", Boolean.parseBoolean(active));

		if (moduleID != null)
			conditions.put("moduleID", moduleID);

		return bean.page(first, limit, sort, dir, conditions, null);
	}

	@JSON
	@RequestMapping(value = "/system/rule/list.json")
	public List<Rule> list(
			@RequestParam(value = "sort", required = false) String sort,
			@RequestParam(value = "dir", required = false) String dir,
			@RequestParam(value = "funID", required = false) String funID,
			@RequestParam(value = "active", required = false) String active,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		Map<String, Object> conditions = new HashMap<String, Object>();
		if (active != null)
			conditions.put("active", Boolean.parseBoolean(active));

		conditions.put("funID", funID);

		return bean.list(sort, dir, conditions, null);
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Rule.class, new JSONEditor(Rule.class));
	}
}
